// pages/orderlist/orderlist.js
var app = getApp();
var url = app.globalData.url;
var userId;
var openId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    URL: app.globalData.url,
    orderlist:[],
    select:"select_type",
    orderType:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    userId = wx.getStorageSync('user_id');
    openId = wx.getStorageSync('open_id');
    // console.log(getCurrentPages());
    wx.setNavigationBarTitle({
      title: '订单列表'
    })
    // console.log(options);
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method:"POST",
      data: {
        user_id: userId,
        type: options.type,
      },
      success:function(res){
        console.log(res);
        for (var i in res.data){
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list){
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        if (options.type == 'WAITPAY'){
          that.setData({
            text:'付款',
          })
        } else if (options.type == 'WAITSEND'){
          that.setData({
            text: '发货',
          })
        } else if (options.type == 'WAITRECEIVE'){
          that.setData({
            text: '收货',
          })
        } else if (options.type == 'WAITCCOMMENT'){
          that.setData({
            text: '评论',
          })
        }
        that.setData({
          orderlist: res.data,
          orderType: options.type,
        });
      } 
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  ordermsg:function(r){
    // console.log(r.currentTarget.id);
    var order_id = r.currentTarget.id;
    wx.navigateTo({
      url: '/pages/ordermsg/ordermsg?order_id=' + order_id,
    })
  },
  submit_payment:function(o){
    // console.log(o.currentTarget.dataset.order_id);
    // console.log(openId);
    var that = this;
    wx.request({
      url: url + '/api/payment/getxcxPay',
      data:{
        openid: openId,
        // user_id:userId,
        pay_radio:'pay_code=weixinsmall',
        order_id: o.currentTarget.dataset.order_id,
      },
      success:function(res){
        // console.log(res.data.timeStamp);
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': 'prepay_id='+res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              wx.request({
                url: url + '/api/user/order_list',
                method: "POST",
                data: {
                  user_id: userId,
                  type: 'WAITRECEIVE',
                },
                success: function (res) {
                  console.log(res);
                  for (var i in res.data) {
                    res.data[i]['order_goods_num'] = 0;
                    for (var n in res.data[i].goods_list) {
                      res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
                      // console.log(res.data[i]['order_goods_num']);
                    }
                  }
                  that.setData({
                    orderlist: res.data,
                    orderType: 'WAITRECEIVE',
                  });
                }
              })
            },
            'fail': function (res) {
              console.log(res);
              wx.showToast({
                title: '已取消支付',
                icon: 'success',
                duration: 2000
              })
            }
          })
      }
    });
  },
  /**
  *全部订单
  */
  allOrder:function(){
    this.setData({
      orderType: "",
    })
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method: "POST",
      data: {
        user_id: userId,
        type: "",
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data) {
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list) {
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        that.setData({
          orderlist: res.data,
          orderType: "",
        });
      }
    })
  },
  /**
   * 待支付订单
  */
  waitPay:function(){
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method: "POST",
      data: {
        user_id: userId,
        type: "WAITPAY",
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data) {
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list) {
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        that.setData({
          orderlist: res.data,
          orderType: "WAITPAY",
        });
      }
    })
  },
  /**
   * 待发货订单
   */
  waitsend:function(){
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method: "POST",
      data: {
        user_id: userId,
        type: "WAITSEND",
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data) {
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list) {
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        that.setData({
          orderlist: res.data,
          orderType: "WAITSEND",
        });
      }
    })
  },
  /**
   * 待收货订单
   */
  waitTake:function(){
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method: "POST",
      data: {
        user_id: userId,
        type: "WAITRECEIVE",
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data) {
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list) {
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        that.setData({
          orderlist: res.data,
          orderType: "WAITRECEIVE",
        });
      }
    })
  },
  /**
   * 待评论订单
   */
  waitComment:function(){
    var that = this;
    wx.request({
      url: url + '/api/user/order_list',
      method: "POST",
      data: {
        user_id: userId,
        type: "WAITCCOMMENT",
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data) {
          res.data[i]['order_goods_num'] = 0;
          for (var n in res.data[i].goods_list) {
            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
            // console.log(res.data[i]['order_goods_num']);
          }
        }
        that.setData({
          orderlist: res.data,
          orderType: "WAITCCOMMENT",
        });
      }
    })
  },
  /**
   * 取消订单
   */
  cancelOrder:function(e){
    var that = this;
    console.log(e);
    wx.request({
      url: url + '/api/user/cancel_order',
      method:'POST',
      data:{
        order_id: e.currentTarget.dataset.order_id,
        user_id:userId,
      },
      success:function(res){
        console.log(res);
        if (res.data.code == 200){
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            success:function(){
              wx.request({
                url: url + '/api/user/order_list',
                method: "POST",
                data: {
                  user_id: userId,
                  type: e.currentTarget.dataset.order_type,
                },
                success: function (res) {
                  console.log(res);
                  for (var i in res.data) {
                    res.data[i]['order_goods_num'] = 0;
                    for (var n in res.data[i].goods_list) {
                      res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
                      // console.log(res.data[i]['order_goods_num']);
                    }
                  }
                  that.setData({
                    orderlist: res.data,
                    orderType: e.currentTarget.dataset.order_type,
                  });
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 确认收货
   */
  confireTake:function(c){
    // console.log(c.currentTarget.dataset.order_id)
    var that =this;
    wx.showModal({
      title: '提示',
      content: '确认收货',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: url + '/api/user/order_confirm',
            method: 'POST',
            data: {
              order_id: c.currentTarget.dataset.order_id,
              user_id: userId,
            },
            success: function (res) {
              console.log(res);
              if (res.data.code == 200) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'success',
                  duration: 2000,
                  success: function () {
                    wx.request({
                      url: url + '/api/user/order_list',
                      method: "POST",
                      data: {
                        user_id: userId,
                        type: c.currentTarget.dataset.order_type,
                      },
                      success: function (res) {
                        console.log(res);
                        for (var i in res.data) {
                          res.data[i]['order_goods_num'] = 0;
                          for (var n in res.data[i].goods_list) {
                            res.data[i]['order_goods_num'] += parseInt(res.data[i].goods_list[n]['goods_num']);
                            // console.log(res.data[i]['order_goods_num']);
                          }
                        }
                        that.setData({
                          orderlist: res.data,
                          orderType: c.currentTarget.dataset.order_type,
                        });
                      }
                    })
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  checkShipping:function(e){
    wx.navigateTo({
      url: '/pages/shipping/shipping?order_id= ' + e.currentTarget.dataset.order_id,
    })
  }
})